package com.kc.sjwh.other;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

import com.kc.sjwh.entity.bo.ResBo;
import com.kc.sjwh.entity.po.Res;

import net.duguying.pinyin.Pinyin;
import net.duguying.pinyin.PinyinException;

/**
 * 工具类
 * date: 2017年5月17日
 * @author zhangGuihua(hua7381@163.com)
 */
public class Tool {
	
	/**
	 * 获取uuid
	 * @return
	 */
	public static String getUuid() {
		return UUID.randomUUID().toString().toUpperCase().replace("-", "");
	}
	
	/**
	 * 向右削减
	 * @param str
	 * @param by
	 * @return
	 */
	public static String rightTrim(String str, String by) {
		String res = str;
		while(res.endsWith(by)) {
			res = res.substring(0, res.length()-by.length());
		}
		return res;
	}
	/**
	 * 向右补全
	 * @param str
	 * @param len
	 * @param by
	 * @return
	 */
	public static String rightPad(String str, int len, String by) {
		String res = str;
		while(res.length() < len) {
			res += by;
		}
		return res;
	}
	
	/**
	 * 将中文翻译为拼音首字母
	 * @param str
	 * @return
	 */
	public static String transToPinyin(String str) {
		String res = "";
		try {
			Pinyin pinyin = new Pinyin();
			for(String s : pinyin.translateInArrayNoMark(str)) {
				res += s.substring(0,1);
			}
		} catch (PinyinException e) {
			e.printStackTrace();
		}
		return res.toUpperCase();
	}
	
	
	/**
	 * 将菜单转化为html(ZUI适用)
	 * @param ress
	 * @return
	 */
	public static String menusToHtml(List<Res> ress) {
		StringBuffer sb = new StringBuffer();
		
		//转bo
		List<ResBo> menus = new ArrayList<ResBo>();
		for(Res res : ress) {
			ResBo m = (ResBo)res;
			m.setSubRess(new ArrayList<ResBo>());
			menus.add(m);
		}
		
		for(ResBo m : menus) {
			for(ResBo m2 : menus) {
				if(m.getId().equals(m2.getFjid())) {
					m.getSubRess().add(m2);
				}
			}
		}
		
		List<ResBo> menus2 = new ArrayList<ResBo>();
		for(ResBo m : menus) {
			if(StringUtils.isEmpty(m.getFjid())) {
				menus2.add(m);
			}
		}
		
		
		for(ResBo m : menus2) {
			if(m.getSubRess().size() == 0) {
				sb.append("<li><a target='iframe_1' href='"+m.getDz()+"'><i class='icon icon-"+m.getTb()+"'></i> "+m.getMc()+"</a></li>");
			} else {
				sb.append("<li class='dropdown'>");
				sb.append("  <a class='dropdown-toggle' data-toggle='dropdown'><i class='icon icon-"+m.getTb()+"'></i> "+m.getMc()+" <b class='caret'></b></a>");
				sb.append("  <ul class='dropdown-menu' role='menu'>");
				for(ResBo m2 : m.getSubRess()) {
					sb.append(" <li><a target='iframe_1' href='"+m2.getDz()+"'>"+m2.getMc()+"</a></li>");
				}
				sb.append("  </ul>");
				sb.append("</li>");
			}
		}
		
		return sb.toString();
	}

	/**
	 * 发送post请求
	 * @param url
	 * @param param
	 */
	public static String sendPost(String url, String param) {
		String result = "";
		PrintWriter out = null;
		BufferedReader in = null;
		try {
			URL realUrl = new URL(url);
			// 打开连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //设置超时时间
            conn.setConnectTimeout(1000*30);
            conn.setReadTimeout(1000*30);
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 读取响应
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			return result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {// 关闭输出流、输入流
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public static String formateDate(Date d) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
	}

	public static Object parseDate(String v) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(v);
		} catch (ParseException e) {
			return null;
		}
	}
}
